<?php


namespace app\model;


use think\facade\Db;
use think\Model;
use app\model\RemarkModel;
use app\model\Attachment as AttachmentModel;
use app\model\DeliveryModel;

class CommentModel extends Model
{
    protected $table = "store_comment";


    public function evaluate($param)
    {
        Db::startTrans();
        try {
            $RemarkModel = new RemarkModel();
            $AttachmentModel = new AttachmentModel();
            $date = date('Y-m-d H:i:s');
            $evaluate = $param['evaluate'];
            $delivery_id = $param['delivery_id']??'';

            foreach ($evaluate as $key => &$val) {
                if(empty($delivery_id)){
                    $delivery_id = $val['delivery_id'];
                }

                $d['delivery_id'] = $val['delivery_id'];
                $d['user_id'] = $val['user_id'];
                $d['service_user_id'] = $val['service_user_id'];
                $d['score'] = $val['score'];
                $d['brand_id'] = $val['brand_id'];
                $d['create_at'] = $date;
                $insertGetId = self::insertGetId($d);

                $str = '';
                if ($val['images']) {
                    $list_arr = explode(',', $val['images']);
                    foreach ($list_arr as $k => &$v) {
                        $str .= $AttachmentModel->insertGetId(['user_id' => $val['user_id'], 'table_name' => $this->table, 'table_id' => $insertGetId, 'path' => $v]) . ',';
                    }
                    $str = trim($str, ',');
                }

                $content_id = 0;
                if (!empty($val['content'])) {
                    $content_id = $RemarkModel::remark_id($val['content'], $this->table, $insertGetId);
                }

                self::where(["id" => $insertGetId])->update(['attachment_id' => $str, 'content_id' => $content_id]);
            }

            $DeliveryAttachModel = new DeliveryAttachModel();
            $DeliveryAttachModel->evaluate($delivery_id);

            Db::commit();
            return 1;
        } catch (\Exception $e) {
            Db::rollback();
            return '';
        }


    }

}